package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.core.code.cmd.RbacCmdCode;
import com.ymk.bo.rbac.RbacPermBO;
import com.ymk.service.IRbacPermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class RbacPermDelCmd extends ACmd {

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        RbacPermBO reqData = super.getData(sign, RbacPermBO.class);

        RbacPermBO rbacPermBO = new RbacPermBO();
        rbacPermBO.setId(reqData.getId());
        this.rbacPermService.del(rbacPermBO);

        rbacPermBO.setId(null);
        rbacPermBO.setParentId(reqData.getId());
        this.rbacPermService.del(rbacPermBO);

        return respSign;
    }

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        RbacPermBO reqData = super.getData(sign, RbacPermBO.class);

        RbacPermBO rbacPermBO = new RbacPermBO();
        rbacPermBO.setId(reqData.getId());
        this.rbacPermService.del(rbacPermBO);

        rbacPermBO.setId(null);
        rbacPermBO.setParentId(reqData.getId());
        this.rbacPermService.del(rbacPermBO);

        return respSign;
    }

    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {
        RbacPermBO reqData = super.getData(sign, RbacPermBO.class);
        if (reqData.getId() == null){
            throw new MallException("Id不能为空!");
        }
        return null;
    }

    @Override
    public String getCmdCode() {
        return RbacCmdCode.RBAC_PERM_DEL;
    }

    @Autowired
    private IRbacPermService rbacPermService;

}
